import utils from './utils'
const crypto = require('crypto');
import RSA from './rsa'
let dev = false
let uat = false
// #ifdef MP-WEIXIN
dev = false
// #endif
let httpApiServer = dev ? '' : 'https://api.yezi-yezi.com'
const getToken = (type,method,params) => {
	let context = `${type}\n${method}\n${params}\n`
	let token = RSA.Encrypt(context)
	return token
}
	
const md5 = (content) => {
	const md5 = crypto.createHash('md5')
	return md5.update(content).digest('hex')
}
	
export const get = (api, url, params) => {
	return new Promise((resolve, reject) => {
		let getServer = (url,params,cb,error) => {
			let headers = {
				'wx_user':uni.getStorageSync('userInfo')?.wx_user
			}
			let wx_user_token = md5(utils.user_token_key + headers.wx_user)
			headers.wx_user_token = wx_user_token
			let newParams = {...params}
			for(let item in newParams){
				if(newParams[item] === undefined){
					newParams[item] = ''	
				}else{
					newParams[item] = newParams[item].toString()
				}
			}
			headers.token = getToken('GET',url,JSON.stringify(newParams))
			console.log(url,'geturl')
			const apiServer = uni.request({
				url: httpApiServer + url,
				method: 'get',
				data:params,
				header:headers,
				timeout:1000 * 10,
				success: (res) => {
					cb(res)
				},
				fail:(err) => {
					error && error(err)
				}
			})
			return apiServer
		}
		
		let apiBefore = api ? getApp().globalData[api] : ''
		console.log(apiBefore,'apiBefore')
		getServer((apiBefore || '') + url, params, (res) => {
			console.log(res,'result')
			if((res.data && res.data.code != 0) || !res.data){
				if(res.data){
					if(res.data.data){
						reject(res.data.data.error || res.data.data.message)
					}else{
						reject(res.data.message)
					}
				}else{
					reject(res.errMsg)
				}
			}else{
				resolve(res.data.data)
			}
		}, (err) => {
			reject(err)
		})
	})
}

const getAPI = (cb) => {
	let proApi = '/payconsumption_uat'
	getApp().globalData.baseURL = proApi
	cb && cb()
}

/**
 * 获取用户唯一标识
 * @param {Object} cb
 */
export const getUserProfile = (cb) => {
	if(!getApp().globalData.baseURL){
		getAPI(() => {
			cb()
		})
	}else{
		cb()
	}
}
export const post = (api, url, params) => {
	return new Promise((resolve, reject) => {
		let post = (url,params,cb,error,headers) => {
			headers = headers || {}
			setTimeout(() => {
				let newParams = {}
				for(let item in params){
					newParams[item] = params[item]
					if(newParams[item] && typeof(newParams[item]) === 'string' && newParams[item].length > 200){
						newParams[item] = newParams[item].substring(0,200)
					}
				}
				headers.token = getToken('POST',url,JSON.stringify(newParams))
				headers.wx_user = uni.getStorageSync('userInfo')?.wx_user || 'oGCIM6xIWkOgZ_QxejB3TrMl1-MQ'
				let wx_user_token = md5(utils.user_token_key + headers.wx_user)
				headers.wx_user_token = wx_user_token
				uni.request({
					url: httpApiServer + url,
					method: 'post',
					data:params,
					header:{
						...headers
					},
					timeout:1000 * 10,
					success: (res) => {
						cb && cb(res)
					},
					fail:(err) => {
						error && error(err)
					}
				})
			},50)
		}
		getUserProfile(() => {
			post(getApp().globalData[api] + url, params, (res) => {
				console.log(res,'postdata')
				if((res.data && res.data.code != 0) || !res.data){
					if(res.data){
						if(res.data.data){
							reject(res.data.data.error || res.data.data.message)
						}else{
							reject(res.data.message)
						}
					}else{
						reject(res.errMsg)
					}
				}else{
					resolve(res.data.data)
				}
			}, (err) => {
				reject(err)
			})
		})
	})
}
